﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

 


namespace IR
{
    
    class Indexer  
    {

        Process m_Prc;
        ProcessStartInfo m_Cmd ;
        DirectoryInfo m_Dinfo;
        DirectoryInfo m_Dinfo_Out;
        TermParser m_Parser;
        string currentDrct;
        Index m_Index;
        int m_num_of_total_docs;
        bool m_Ready;

        public bool Ready
        {
            get { return m_Ready; }
            set { m_Ready = value; }
        }
        public IR.Index Index
        {
            get { return m_Index; }
            set { m_Index = value; }
        }
        public int Num_of_total_docs
        {
            get { return m_num_of_total_docs; }
            set { m_num_of_total_docs = value; }
        }
        

        public Indexer()
        {
            currentDrct = Environment.CurrentDirectory;
            m_Prc = new Process();
            m_Cmd = new ProcessStartInfo();
            m_Dinfo = new DirectoryInfo(currentDrct+@"\data");
            m_Dinfo_Out = new DirectoryInfo(currentDrct + @"\output");
            
            m_Cmd.FileName = @"cmd";
            m_Cmd.WindowStyle = ProcessWindowStyle.Normal;
            m_Cmd.CreateNoWindow = true;

            m_Cmd.UseShellExecute = false;
            m_Cmd.RedirectStandardOutput = true;
            m_Cmd.RedirectStandardInput = true;
            m_Cmd.RedirectStandardError = true;
            
            m_Prc.EnableRaisingEvents = false;
            m_Prc.StartInfo = m_Cmd;
            m_Parser = new TermParser(@"(.*)/N");

            m_Index = new Index();
            m_Ready = false;
          
        }

        public void ExecuteKMA(string file_name, string outfile_name)
        {   
            m_Prc.Start();
            //m_Prc.StandardInput.Write(@"kma -/xsiw " + currentDrct+ @"\data\" + file_name + " " +  currentDrct +@"\output\" + outfile_name + Environment.NewLine );
            m_Prc.StandardInput.Write(@"kma -/xsiw " + file_name + " " + currentDrct + @"\output\" + outfile_name + Environment.NewLine);
            m_Prc.StandardInput.Write("y" + Environment.NewLine);
            m_Prc.StandardInput.Close();
            m_Prc.Close();
          
        }
        
        
        public void RunIndexer()
        {

            MakeOutputFile();
            MakeIndex();
            m_Ready = true;
                
        }
        private void MakeOutputFile()
        {

             DirectoryInfo Dinfo_input;
             DirectoryInfo Dinfo_output;
             Dinfo_input = new DirectoryInfo(currentDrct+@"\data");
             Dinfo_output = new DirectoryInfo(currentDrct+@"\output");

             //if (Dinfo_input.GetFiles().Length != Dinfo_output.GetFiles().Length)
             //{
                 foreach (FileInfo f in m_Dinfo.GetFiles())
                 {      
                     System.Threading.Thread.Sleep(20);
                     ExecuteKMA(f.FullName, f.Name.Replace(".txt", ".out"));
                      
                 }
             //}
             m_num_of_total_docs = Dinfo_output.GetFiles().Length;
             m_Index.Num_of_total_docs = m_num_of_total_docs;
            
        
        }


        private void MakeIndex()
        {
            
            m_Index.MakeIndex();
        }
         
 
    }
}
